#include "sm2.h"
#include "common_func.h"
#include "core_time.h"
#include <string.h>
#include <stdio.h> 

uint32_t sm2_signature(ECCrefPrivateKey *prikey, uint32_t *random_k, uint32_t *hash_data, ECCSignature *signature)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x0)
	{
		return 0x01;
	}
	
	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x0);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_K_RANDOM_0, random_k[0]);
	RegWrite(SM2_K_RANDOM_1, random_k[1]);
	RegWrite(SM2_K_RANDOM_2, random_k[2]);
	RegWrite(SM2_K_RANDOM_3, random_k[3]);
	RegWrite(SM2_K_RANDOM_4, random_k[4]);
	RegWrite(SM2_K_RANDOM_5, random_k[5]);
	RegWrite(SM2_K_RANDOM_6, random_k[6]);
	RegWrite(SM2_K_RANDOM_7, random_k[7]);
	RegWrite(SM2_PRIVATE_KEY_0, prikey->K[0]);
	RegWrite(SM2_PRIVATE_KEY_1, prikey->K[1]);
	RegWrite(SM2_PRIVATE_KEY_2, prikey->K[2]);
	RegWrite(SM2_PRIVATE_KEY_3, prikey->K[3]);
	RegWrite(SM2_PRIVATE_KEY_4, prikey->K[4]);
	RegWrite(SM2_PRIVATE_KEY_5, prikey->K[5]);
	RegWrite(SM2_PRIVATE_KEY_6, prikey->K[6]);
	RegWrite(SM2_PRIVATE_KEY_7, prikey->K[7]);
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);
	
	//wait result
	while(RegRead(SM2_ECC_DONE) == 0x00)
	{
		delay_ms(SM2_WAIT_TIME);
		//printf("RegRead(SM2_ECC_DONE) == 0x00\n");
	}
	
	if(RegRead(SM2_ERROR_CODE) != 0x0)
	{
		return 0x13;
	}
	
	//复制结果
	signature->r[0] = RegRead(SM2_RESULT_R_0);
	signature->r[1] = RegRead(SM2_RESULT_R_1);
	signature->r[2] = RegRead(SM2_RESULT_R_2);
	signature->r[3] = RegRead(SM2_RESULT_R_3);
	signature->r[4] = RegRead(SM2_RESULT_R_4);
	signature->r[5] = RegRead(SM2_RESULT_R_5);
	signature->r[6] = RegRead(SM2_RESULT_R_6);
	signature->r[7] = RegRead(SM2_RESULT_R_7);
	signature->s[0] = RegRead(SM2_RESULT_S_0);
	signature->s[1] = RegRead(SM2_RESULT_S_1);
	signature->s[2] = RegRead(SM2_RESULT_S_2);
	signature->s[3] = RegRead(SM2_RESULT_S_3);
	signature->s[4] = RegRead(SM2_RESULT_S_4);
	signature->s[5] = RegRead(SM2_RESULT_S_5);
	signature->s[6] = RegRead(SM2_RESULT_S_6);
	signature->s[7] = RegRead(SM2_RESULT_S_7);
	
	//清理
	RegWrite(SM2_ECC_START, 0x0);
	RegWrite(SM2_HASH_DONE, 0x0);
	
	return 0;
}

//需要首先获取密钥使用权限
uint32_t sm2_signature_inkey(uint32_t *random_k, uint32_t *hash_data, ECCSignature *signature)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x0)
	{
		return 0x01;
	}
	
	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x0);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_K_RANDOM_0, random_k[0]);
	RegWrite(SM2_K_RANDOM_1, random_k[1]);
	RegWrite(SM2_K_RANDOM_2, random_k[2]);
	RegWrite(SM2_K_RANDOM_3, random_k[3]);
	RegWrite(SM2_K_RANDOM_4, random_k[4]);
	RegWrite(SM2_K_RANDOM_5, random_k[5]);
	RegWrite(SM2_K_RANDOM_6, random_k[6]);
	RegWrite(SM2_K_RANDOM_7, random_k[7]);
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);
	
	//wait result
	while(RegRead(SM2_ECC_DONE) == 0x00)
	{
		delay_ms(SM2_WAIT_TIME);
		//printf("RegRead(SM2_ECC_DONE) == 0x00\n");
	}
	
	if(RegRead(SM2_ERROR_CODE) != 0x0)
	{
		return 0x13;
	}
	
	//复制结果
	signature->r[0] = RegRead(SM2_RESULT_R_0);
	signature->r[1] = RegRead(SM2_RESULT_R_1);
	signature->r[2] = RegRead(SM2_RESULT_R_2);
	signature->r[3] = RegRead(SM2_RESULT_R_3);
	signature->r[4] = RegRead(SM2_RESULT_R_4);
	signature->r[5] = RegRead(SM2_RESULT_R_5);
	signature->r[6] = RegRead(SM2_RESULT_R_6);
	signature->r[7] = RegRead(SM2_RESULT_R_7);
	signature->s[0] = RegRead(SM2_RESULT_S_0);
	signature->s[1] = RegRead(SM2_RESULT_S_1);
	signature->s[2] = RegRead(SM2_RESULT_S_2);
	signature->s[3] = RegRead(SM2_RESULT_S_3);
	signature->s[4] = RegRead(SM2_RESULT_S_4);
	signature->s[5] = RegRead(SM2_RESULT_S_5);
	signature->s[6] = RegRead(SM2_RESULT_S_6);
	signature->s[7] = RegRead(SM2_RESULT_S_7);
	
	//清理
	RegWrite(SM2_ECC_START, 0x0);
	RegWrite(SM2_HASH_DONE, 0x0);
	
	return 0;
}

uint32_t sm2_signature_int(ECCrefPrivateKey *prikey, uint32_t *random_k, uint32_t *hash_data)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x0)
	{
		return 0x01;
	}
	
	RegWrite(0xbf20f004, 0x80);//edge	边沿
	RegWrite(0xbf20f008, 0x80);//pol  高电平
	RegWrite(0xbf20f00c, 0x80);//clr
	RegWrite(0xbf20f000, 0x80);//en
	RegWrite(SM2_ENABLE_INT, 0x1);//en
	
	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x0);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_K_RANDOM_0, random_k[0]);
	RegWrite(SM2_K_RANDOM_1, random_k[1]);
	RegWrite(SM2_K_RANDOM_2, random_k[2]);
	RegWrite(SM2_K_RANDOM_3, random_k[3]);
	RegWrite(SM2_K_RANDOM_4, random_k[4]);
	RegWrite(SM2_K_RANDOM_5, random_k[5]);
	RegWrite(SM2_K_RANDOM_6, random_k[6]);
	RegWrite(SM2_K_RANDOM_7, random_k[7]);
	RegWrite(SM2_PRIVATE_KEY_0, prikey->K[0]);
	RegWrite(SM2_PRIVATE_KEY_1, prikey->K[1]);
	RegWrite(SM2_PRIVATE_KEY_2, prikey->K[2]);
	RegWrite(SM2_PRIVATE_KEY_3, prikey->K[3]);
	RegWrite(SM2_PRIVATE_KEY_4, prikey->K[4]);
	RegWrite(SM2_PRIVATE_KEY_5, prikey->K[5]);
	RegWrite(SM2_PRIVATE_KEY_6, prikey->K[6]);
	RegWrite(SM2_PRIVATE_KEY_7, prikey->K[7]);
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);
	
	return 0;
}

//需要首先获取密钥使用权限
uint32_t sm2_signature_int_inkey(uint32_t *random_k, uint32_t *hash_data)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x0)
	{
		return 0x01;
	}
	
	RegWrite(0xbf20f004, 0x80);//edge	边沿
	RegWrite(0xbf20f008, 0x80);//pol  高电平
	RegWrite(0xbf20f00c, 0x80);//clr
	RegWrite(0xbf20f000, 0x80);//en
	RegWrite(SM2_ENABLE_INT, 0x1);//en
	
	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x0);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_K_RANDOM_0, random_k[0]);
	RegWrite(SM2_K_RANDOM_1, random_k[1]);
	RegWrite(SM2_K_RANDOM_2, random_k[2]);
	RegWrite(SM2_K_RANDOM_3, random_k[3]);
	RegWrite(SM2_K_RANDOM_4, random_k[4]);
	RegWrite(SM2_K_RANDOM_5, random_k[5]);
	RegWrite(SM2_K_RANDOM_6, random_k[6]);
	RegWrite(SM2_K_RANDOM_7, random_k[7]);
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);
	
	return 0;
}

uint32_t sm2_signature_callback(ECCSignature *signature)
{
	if(RegRead(SM2_ERROR_CODE) != 0x0)
	{
		return 0x13;
	}
	
	//复制结果
	signature->r[0] = RegRead(SM2_RESULT_R_0);
	signature->r[1] = RegRead(SM2_RESULT_R_1);
	signature->r[2] = RegRead(SM2_RESULT_R_2);
	signature->r[3] = RegRead(SM2_RESULT_R_3);
	signature->r[4] = RegRead(SM2_RESULT_R_4);
	signature->r[5] = RegRead(SM2_RESULT_R_5);
	signature->r[6] = RegRead(SM2_RESULT_R_6);
	signature->r[7] = RegRead(SM2_RESULT_R_7);
	signature->s[0] = RegRead(SM2_RESULT_S_0);
	signature->s[1] = RegRead(SM2_RESULT_S_1);
	signature->s[2] = RegRead(SM2_RESULT_S_2);
	signature->s[3] = RegRead(SM2_RESULT_S_3);
	signature->s[4] = RegRead(SM2_RESULT_S_4);
	signature->s[5] = RegRead(SM2_RESULT_S_5);
	signature->s[6] = RegRead(SM2_RESULT_S_6);
	signature->s[7] = RegRead(SM2_RESULT_S_7);
	
	//清理
	RegWrite(SM2_ECC_START, 0x0);
	RegWrite(SM2_HASH_DONE, 0x0);
	
	return 0;
}

uint32_t sm2_verify(ECCrefPublicKey *pubkey, ECCSignature *signature, uint32_t *hash_data)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x000)
	{
		return 0x01;
	}

	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x01);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_PUBKEY_X_0, pubkey->x[0]);
	RegWrite(SM2_PUBKEY_X_1, pubkey->x[1]);
	RegWrite(SM2_PUBKEY_X_2, pubkey->x[2]);
	RegWrite(SM2_PUBKEY_X_3, pubkey->x[3]);
	RegWrite(SM2_PUBKEY_X_4, pubkey->x[4]);
	RegWrite(SM2_PUBKEY_X_5, pubkey->x[5]);
	RegWrite(SM2_PUBKEY_X_6, pubkey->x[6]);
	RegWrite(SM2_PUBKEY_X_7, pubkey->x[7]);
	RegWrite(SM2_PUBKEY_Y_0, pubkey->y[0]);
	RegWrite(SM2_PUBKEY_Y_1, pubkey->y[1]);
	RegWrite(SM2_PUBKEY_Y_2, pubkey->y[2]);
	RegWrite(SM2_PUBKEY_Y_3, pubkey->y[3]);
	RegWrite(SM2_PUBKEY_Y_4, pubkey->y[4]);
	RegWrite(SM2_PUBKEY_Y_5, pubkey->y[5]);
	RegWrite(SM2_PUBKEY_Y_6, pubkey->y[6]);
	RegWrite(SM2_PUBKEY_Y_7, pubkey->y[7]);
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_SIGNATURE_R_0, signature->r[0]);
	RegWrite(SM2_SIGNATURE_R_1, signature->r[1]);
	RegWrite(SM2_SIGNATURE_R_2, signature->r[2]);
	RegWrite(SM2_SIGNATURE_R_3, signature->r[3]);
	RegWrite(SM2_SIGNATURE_R_4, signature->r[4]);
	RegWrite(SM2_SIGNATURE_R_5, signature->r[5]);
	RegWrite(SM2_SIGNATURE_R_6, signature->r[6]);
	RegWrite(SM2_SIGNATURE_R_7, signature->r[7]);
	RegWrite(SM2_SIGNATURE_S_0, signature->s[0]);
	RegWrite(SM2_SIGNATURE_S_1, signature->s[1]);
	RegWrite(SM2_SIGNATURE_S_2, signature->s[2]);
	RegWrite(SM2_SIGNATURE_S_3, signature->s[3]);
	RegWrite(SM2_SIGNATURE_S_4, signature->s[4]);
	RegWrite(SM2_SIGNATURE_S_5, signature->s[5]);
	RegWrite(SM2_SIGNATURE_S_6, signature->s[6]);
	RegWrite(SM2_SIGNATURE_S_7, signature->s[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);
	
	//wait result
	while(RegRead(SM2_ECC_DONE) == 0x00)
	{
		delay_ms(SM2_WAIT_TIME);
	}
	
	if(RegRead(SM2_ERROR_CODE) != 0x0)
	{
		return 0x13;
	}
	
	//获取结果
	uint32_t verify_result = RegRead(SM2_VERIFY_OK);
	
	//清理
	RegWrite(SM2_ECC_START, 0x0);
	RegWrite(SM2_HASH_DONE, 0x0);
	
	//验签成功
	if(verify_result == 0x1)
	{
		return 0;
	}
	else
	{
		return 0x11;
	}
}

//需要首先获取密钥使用权限
uint32_t sm2_verify_inkey(ECCSignature *signature, uint32_t *hash_data)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x000)
	{
		return 0x01;
	}

	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x01);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_SIGNATURE_R_0, signature->r[0]);
	RegWrite(SM2_SIGNATURE_R_1, signature->r[1]);
	RegWrite(SM2_SIGNATURE_R_2, signature->r[2]);
	RegWrite(SM2_SIGNATURE_R_3, signature->r[3]);
	RegWrite(SM2_SIGNATURE_R_4, signature->r[4]);
	RegWrite(SM2_SIGNATURE_R_5, signature->r[5]);
	RegWrite(SM2_SIGNATURE_R_6, signature->r[6]);
	RegWrite(SM2_SIGNATURE_R_7, signature->r[7]);
	RegWrite(SM2_SIGNATURE_S_0, signature->s[0]);
	RegWrite(SM2_SIGNATURE_S_1, signature->s[1]);
	RegWrite(SM2_SIGNATURE_S_2, signature->s[2]);
	RegWrite(SM2_SIGNATURE_S_3, signature->s[3]);
	RegWrite(SM2_SIGNATURE_S_4, signature->s[4]);
	RegWrite(SM2_SIGNATURE_S_5, signature->s[5]);
	RegWrite(SM2_SIGNATURE_S_6, signature->s[6]);
	RegWrite(SM2_SIGNATURE_S_7, signature->s[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);
	
	//wait result
	while(RegRead(SM2_ECC_DONE) == 0x00)
	{
		delay_ms(SM2_WAIT_TIME);
	}
	
	if(RegRead(SM2_ERROR_CODE) != 0x0)
	{
		return 0x13;
	}
	
	//获取结果
	uint32_t verify_result = RegRead(SM2_VERIFY_OK);
	
	//清理
	RegWrite(SM2_ECC_START, 0x0);
	RegWrite(SM2_HASH_DONE, 0x0);
	
	//验签成功
	if(verify_result == 0x1)
	{
		return 0;
	}
	else
	{
		return 0x11;
	}
}

uint32_t sm2_verify_int(ECCrefPublicKey *pubkey, ECCSignature *signature, uint32_t *hash_data)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x000)
	{
		return 0x01;
	}
	
	RegWrite(0xbf20f004, 0x80);//edge	边沿
	RegWrite(0xbf20f008, 0x80);//pol  高电平
	RegWrite(0xbf20f00c, 0x80);//clr
	RegWrite(0xbf20f000, 0x80);//en
	RegWrite(SM2_ENABLE_INT, 0x1);//en
	
	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x01);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_PUBKEY_X_0, pubkey->x[0]);
	RegWrite(SM2_PUBKEY_X_1, pubkey->x[1]);
	RegWrite(SM2_PUBKEY_X_2, pubkey->x[2]);
	RegWrite(SM2_PUBKEY_X_3, pubkey->x[3]);
	RegWrite(SM2_PUBKEY_X_4, pubkey->x[4]);
	RegWrite(SM2_PUBKEY_X_5, pubkey->x[5]);
	RegWrite(SM2_PUBKEY_X_6, pubkey->x[6]);
	RegWrite(SM2_PUBKEY_X_7, pubkey->x[7]);
	RegWrite(SM2_PUBKEY_Y_0, pubkey->y[0]);
	RegWrite(SM2_PUBKEY_Y_1, pubkey->y[1]);
	RegWrite(SM2_PUBKEY_Y_2, pubkey->y[2]);
	RegWrite(SM2_PUBKEY_Y_3, pubkey->y[3]);
	RegWrite(SM2_PUBKEY_Y_4, pubkey->y[4]);
	RegWrite(SM2_PUBKEY_Y_5, pubkey->y[5]);
	RegWrite(SM2_PUBKEY_Y_6, pubkey->y[6]);
	RegWrite(SM2_PUBKEY_Y_7, pubkey->y[7]);
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_SIGNATURE_R_0, signature->r[0]);
	RegWrite(SM2_SIGNATURE_R_1, signature->r[1]);
	RegWrite(SM2_SIGNATURE_R_2, signature->r[2]);
	RegWrite(SM2_SIGNATURE_R_3, signature->r[3]);
	RegWrite(SM2_SIGNATURE_R_4, signature->r[4]);
	RegWrite(SM2_SIGNATURE_R_5, signature->r[5]);
	RegWrite(SM2_SIGNATURE_R_6, signature->r[6]);
	RegWrite(SM2_SIGNATURE_R_7, signature->r[7]);
	RegWrite(SM2_SIGNATURE_S_0, signature->s[0]);
	RegWrite(SM2_SIGNATURE_S_1, signature->s[1]);
	RegWrite(SM2_SIGNATURE_S_2, signature->s[2]);
	RegWrite(SM2_SIGNATURE_S_3, signature->s[3]);
	RegWrite(SM2_SIGNATURE_S_4, signature->s[4]);
	RegWrite(SM2_SIGNATURE_S_5, signature->s[5]);
	RegWrite(SM2_SIGNATURE_S_6, signature->s[6]);
	RegWrite(SM2_SIGNATURE_S_7, signature->s[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);

	return 0;
}

//需要首先获取密钥使用权限
uint32_t sm2_verify_int_inkey(ECCSignature *signature, uint32_t *hash_data)
{
	// module not ready for input
	if(RegRead(SM2_ECC_STATE) != 0x000)
	{
		return 0x01;
	}
	
	RegWrite(0xbf20f004, 0x80);//edge	边沿
	RegWrite(0xbf20f008, 0x80);//pol  高电平
	RegWrite(0xbf20f00c, 0x80);//clr
	RegWrite(0xbf20f000, 0x80);//en
	RegWrite(SM2_ENABLE_INT, 0x1);//en
	
	//写入数据
	RegWrite(SM2_ECC_FUNC_SELECT, 0x01);
	RegWrite(SM2_ECC_CURVE_SELECT, 0x01);	//sm2
	RegWrite(SM2_HASH_RESULT_0, hash_data[0]);
	RegWrite(SM2_HASH_RESULT_1, hash_data[1]);
	RegWrite(SM2_HASH_RESULT_2, hash_data[2]);
	RegWrite(SM2_HASH_RESULT_3, hash_data[3]);
	RegWrite(SM2_HASH_RESULT_4, hash_data[4]);
	RegWrite(SM2_HASH_RESULT_5, hash_data[5]);
	RegWrite(SM2_HASH_RESULT_6, hash_data[6]);
	RegWrite(SM2_HASH_RESULT_7, hash_data[7]);
	RegWrite(SM2_SIGNATURE_R_0, signature->r[0]);
	RegWrite(SM2_SIGNATURE_R_1, signature->r[1]);
	RegWrite(SM2_SIGNATURE_R_2, signature->r[2]);
	RegWrite(SM2_SIGNATURE_R_3, signature->r[3]);
	RegWrite(SM2_SIGNATURE_R_4, signature->r[4]);
	RegWrite(SM2_SIGNATURE_R_5, signature->r[5]);
	RegWrite(SM2_SIGNATURE_R_6, signature->r[6]);
	RegWrite(SM2_SIGNATURE_R_7, signature->r[7]);
	RegWrite(SM2_SIGNATURE_S_0, signature->s[0]);
	RegWrite(SM2_SIGNATURE_S_1, signature->s[1]);
	RegWrite(SM2_SIGNATURE_S_2, signature->s[2]);
	RegWrite(SM2_SIGNATURE_S_3, signature->s[3]);
	RegWrite(SM2_SIGNATURE_S_4, signature->s[4]);
	RegWrite(SM2_SIGNATURE_S_5, signature->s[5]);
	RegWrite(SM2_SIGNATURE_S_6, signature->s[6]);
	RegWrite(SM2_SIGNATURE_S_7, signature->s[7]);
	RegWrite(SM2_HASH_DONE, 0x1);
	RegWrite(SM2_ECC_START, 0x1);

	return 0;
}

uint32_t sm2_verify_callback(void)
{
	if(RegRead(SM2_ERROR_CODE) != 0x0)
	{
		return 0x13;
	}
	
	//获取结果
	uint32_t verify_result = RegRead(SM2_VERIFY_OK);
	
	//清理
	RegWrite(SM2_ECC_START, 0x0);
	RegWrite(SM2_HASH_DONE, 0x0);
	
	//验签成功
	if(verify_result == 0x1)
	{
		return 0;
	}
	else
	{
		return 0x11;
	}
}
